/*****************************************************************************/ /*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _SYSTASK_MODULE_
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "common.h"
#include "swap.h"
#include "bsp_spi_nor.h"
#include "nand_w25n.h"
#include "bsp_api.h"
#include "mbedtls\aes.h"
#include "mem_alloc.h"

void sw_iic_opt(void);
void sys_flash_init(void);
void sys_led_ctr(void);
void nor_flash_demo(void);
void sys_tick_exe(void);

/*****************************************************************************
*
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(void *argument)
{
	nor_spi_init();

	sys_flash_init();

	app_timer_task_add(sys_tick_exe);

	mem_ccm.api->malloc(64);

	while (1)
	{
		sys_st.info.stack_used = stack_used();

		bsp_dwt_check();

		nor_flash_demo();

		sys_st.u_tick = app_tick_get();

		// 虚拟RTC
		pulse_create(&sys_st.pulse, sys_st.u_tick);
		if (sys_st.pulse.pulse_1s)
		{
			TimeRun(&sys_st.rtc_time);
		}

		// LED
		if (sys_st.pulse.pulse_10ms)
			sys_led_ctr();

		// sd check
		io_flt(&sys_st.bios.sd_cd, 40, sd_cd.in_st());

		NOP1();
		osDelay(ms_ticks(10));
		NOP1();
	}
}

/*****************************************************************************
*
* @brief   led control.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_led_ctr(void)
{
	static word_tt st = {1};

	// 位移
	if (sys_st.pulse.pulse_100ms)
	{
		st.uval = bit16_roll_shift_left(st.uval, 4, 1);
	}

	// 输出
	if (st.bits.b0)
		o_led1.en();
	else
		o_led1.dis();

	if (st.bits.b1)
		o_led2.en();
	else
		o_led2.dis();
}

/*****************************************************************************
* @brief   flash opt.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_flash_init(void)
{
	// software reset chip
	nor_cmd_exe(SPI_NOR_CMD_Enable_Reset);
	nor_cmd_exe(SPI_NOR_CMD_Reset);

	// wait
	nor_wait_for_busy();

	// read ID
	nor_id_read(sys_st.flash.nor_id);
	dbg_u8_print("nor  id :", sys_st.flash.nor_id, 4, 10);

	// read status
	sys_st.flash.nor_sr[0] = nor_reg_get(1);
	sys_st.flash.nor_sr[1] = nor_reg_get(2);
	sys_st.flash.nor_sr[2] = nor_reg_get(3);

	nor_read(0, 128, sys_st.flash.buff);

	// uuid
	sys_st.flash.buff[0] = 0x4B;
	loop(4) sys_st.flash.buff[1+index] = 0xff;
	nor_cmd_auto(sys_st.flash.buff, 5, sys_st.flash.buff, 8);
	dbg_u8_print("NOR UUID: ", sys_st.flash.buff, 8, 10);
	
	// nand info config

	spi_nand_info.block_total = 1024;
	spi_nand_info.page_main_size = 2048;
	spi_nand_info.page_spare_size = 16;
	spi_nand_info.page_per_block = 64;
	spi_nand_reset();
	gd5_init();
	spi_nand_read_id(sys_st.flash.nand_id);
	dbg_u8_print("nand id :", sys_st.flash.nand_id, 4, 10);

	NOP1();
}

/*****************************************************************************
* @brief   iic opt.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sw_iic_opt(void)
{
	uu8 eepw[8];
	uu8 eepr[8];

	sw_iic_init(sw_iic);

	// fill data
	loop_n(8)
	{
		eepw[index] = index;
	}

	// send data to eeprom
	sw_iic_chip_send_str(sw_iic, 0xA0, 0, eepw, 4);

	// delay
	osDelay(ms_ticks(10));

	// read data from eeprom
	sw_iic_chip_rcv_str(sw_iic, 0xA0, 0, eepr, 4);

	NOP1();
}

/*****************************************************************************
*
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_sw_hook(char pos)
{
}

/*****************************************************************************
*
* @brief   sys_tick_exe
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_tick_exe(void)
{
}

/*****************************************************************************
*
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(char pos)
{
}
